<?php


namespace app\system\controller;

class ApiCode extends AdminBase
{
	public function getlist()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\ApiCodeService::getlist($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function getinfo($id)
	{
		$options = ["id" => $id];
		$result = \app\system\service\ApiCodeService::getinfo($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["type" => "require", "api_id" => "require", "message" => "require"], ["type.require" => "type 不能为空", "api_id.require" => "api_id 不能为空", "message.require" => "message 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\ApiCodeService::add($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "type" => "require", "api_id" => "require", "message" => "require"], ["id.require" => "id 不能为空", "type.require" => "type 不能为空", "api_id.require" => "api_id 不能为空", "message.require" => "message 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\ApiCodeService::edit($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del()
	{
		$options = $this->request->post();
		$result = \app\system\service\ApiCodeService::del($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_app_config($code_id, $appid)
	{
		$options = ["code_id" => $code_id, "appid" => $appid];
		$result = \app\system\service\ApiCodeService::get_app_config($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function set_app_config()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["code_id" => "require", "appid" => "require"], ["code_id.require" => "code_id 不能为空", "appid.require" => "appid 不能为空", "id.require" => "业务码 不能为空", "message.require" => "业务信息 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\ApiCodeService::set_app_config($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "设置成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}